package com.jzo2o.orders.manager.service;

import cn.hutool.extra.spring.SpringUtil;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Map;

@Component
public class PayStrategyManager {
    Map<String,PayStrategy> payStrategyMap;

    //维护所有策略对象
    @PostConstruct
    public  void  init(){
        payStrategyMap = SpringUtil.getBeansOfType(PayStrategy.class);
    }

    //根据用户需求,执行指定的策略对象
    public void pay(String key){
        PayStrategy payStrategy = payStrategyMap.get(key);
        if (payStrategy == null){
            throw new ForbiddenOperationException("暂不支持当前支付方式");
        }
        payStrategy.pay();
    }
}
